package com.kedou.sdk.utils;

import android.text.TextUtils;

import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.core.HttpEngineConduits;
import com.kedou.sdk.download.VersionUpdateDownloader;
import com.kedou.sdk.params.bean.VersionInfo;

public final class CommonUtils {

    private CommonUtils() {
    }

    /**
     * 是否有新版本更新
     *
     * @return 有新版本更新返回true，否则返回false
     */
    public static boolean isUpdateVersion() {
        boolean updateVersion = false;
        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
        VersionInfo newVersionInfo = conduits.readVersionInfo();

        if (!TextUtils.isEmpty(newVersionInfo.getUrl())) {
            String code = XmlUtils.getChannelGameVersionCode();
            final int oldVersionCode = ToolsUtils.isNumberType(code) ? Integer.parseInt(code) : 1;
            updateVersion = oldVersionCode < newVersionInfo.getVersionCode();
        }

        if (updateVersion) {
            if (VersionUpdateDownloader.getImpl().isDownloading()) {
                updateVersion = false;
            }
        }

        return updateVersion;
    }

    /**
     * 是否强制更新版本
     *
     * @return 如果需要强制更新版本返回true，否则返回false
     */
    public static boolean isForceUpdateVersion() {
        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
        VersionInfo newVersionInfo = conduits.readVersionInfo();
        return newVersionInfo.isForceUpdate();
    }

    /**
     * 是否存在已经下载好了的apk版本文件
     *
     * @return
     */
    public static boolean existsDownloadedVersion() {
        String code = XmlUtils.getChannelGameVersionCode();
        final int oldVersionCode = ToolsUtils.isNumberType(code) ? Integer.parseInt(code) : 1;

        VersionUpdateDownloader vum = VersionUpdateDownloader.getImpl();
        final int downloadedVersionCode = vum.getDownloadedVersionCode();

        return downloadedVersionCode > oldVersionCode && vum.checkDownloadFileCompletedStatus();
    }

    /**
     * 是否支持展示广告
     *
     * @return
     */
    public static boolean isSupportAdvertising() {
//        HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
//        if (conduits != null) {
//            ConfigInfo configInfo = conduits.getPlatformConfigInfo();
//            return configInfo != null && configInfo.isSupportAdvert();
//        }
        return false;
    }

}
